﻿package com.vicentediaz.view.imageselector
{
    import flash.display.*;
    import flash.events.*;

    public class ImageSelectorSlider extends Sprite
    {
        private var left:Number;
        private const BASE_ALPHA:Number = 1;
        private var base:Sprite;
        private const BASE_COLOR:uint = 2236962;
        private var right:Number;
        private var slider:Sprite;
        private var offMouseX:Number;
        public var scrubbing:Boolean;
        private var _value:Number = 0;

        public function ImageSelectorSlider()
        {
            this.base = new Sprite();
            this.slider = new Sprite();
            this.slider.addChild(new Bitmap(new ImageSelectorSliderShape(65, 5), "auto", false));
            this.slider.buttonMode = true;
            addChildAt(this.base, 0);
            addChild(this.slider);
            this.slider.x = 1;
            this.slider.y = 1;
            this.slider.addEventListener(MouseEvent.MOUSE_DOWN, this.onSliderMouseDown, false, 0, true);
            this.base.addEventListener(MouseEvent.MOUSE_DOWN, this.onBaseMouseDown, false, 0, true);
            this.width = 150;
            return;
        }// end function

        public function get value() : Number
        {
            return this._value;
        }// end function

        override public function get width() : Number
        {
            return super.width;
        }// end function

        override public function set width(param1:Number) : void
        {
            this.drawTriangledBar(param1);
            this.left = this.base.x + 1;
            this.right = (this.base.x + this.base.width - this.slider.width)--;
            this.updateFromValue(true);
            return;
        }// end function

        private function onSliderMouseUp(param1:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onSliderMouseMove);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.onSliderMouseUp);
            this.scrubbing = false;
            return;
        }// end function

        private function onSliderMouseMove(param1:MouseEvent) : void
        {
            this.moveTo(mouseX - this.offMouseX);
            return;
        }// end function

        private function updateFromValue(param1:Boolean = false) : void
        {
            this.slider.x = int(this.left + this._value * (this.right - this.left));
            if (!param1)
            {
                this.dispatch();
            }// end if
            return;
        }// end function

        private function onSliderMouseDown(param1:MouseEvent) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onSliderMouseMove, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onSliderMouseUp, false, 0, true);
            this.offMouseX = this.slider.mouseX;
            this.scrubbing = true;
            return;
        }// end function

        private function moveTo(param1:Number) : void
        {
            this.slider.x = param1;
            this.slider.x = this.slider.x < this.left ? (this.left) : (this.slider.x);
            this.slider.x = this.slider.x > this.right ? (this.right) : (this.slider.x);
            this.updateFromUser();
            return;
        }// end function

        public function set value(param1:Number) : void
        {
            param1 = param1 > 1 ? (1) : (param1 < 0 ? (0) : (param1));
            this._value = param1;
            this.updateFromValue();
            return;
        }// end function

        private function onBaseMouseDown(param1:MouseEvent) : void
        {
            this.moveTo(mouseX - this.slider.width * 0.5);
            this.onSliderMouseDown(null);
            return;
        }// end function

        private function dispatch() : void
        {
            dispatchEvent(new Event(Event.CHANGE));
            return;
        }// end function

        private function drawTriangledBar(param1:Number) : void
        {
            this.base.graphics.clear();
            this.base.graphics.beginFill(this.BASE_COLOR, this.BASE_ALPHA);
            this.base.graphics.drawRect(2, 0, param1 - 4, 7);
            this.base.graphics.endFill();
            this.base.graphics.beginFill(this.BASE_COLOR, this.BASE_ALPHA);
            this.base.graphics.drawRect(0, 2, param1, 3);
            this.base.graphics.endFill();
            return;
        }// end function

        private function updateFromUser() : void
        {
            this._value = (this.slider.x - this.left) / (this.right - this.left);
            this.dispatch();
            return;
        }// end function

    }
}
